﻿Imports Microsoft.VisualBasic
Imports System.Data.OleDb
Public Class Utility
    Public Shared Function GetValidInt(ByVal obj As Object, _
               Optional ByVal defaultValue As Integer = 0) As Integer
        Dim i As Integer
        Try
            If (obj Is Nothing) Then
                i = defaultValue
            Else
                i = Convert.ToInt32(obj)
            End If
        Catch ex As Exception
            i = defaultValue
        End Try

        Return i
    End Function
    Public Shared Function GetValidDouble(ByVal obj As Object, _
                   Optional ByVal defaultValue As Double = 0) As Double
        Dim i As Double
        Try
            If (obj Is Nothing) Then
                i = defaultValue
            Else
                i = Convert.ToDouble(obj)
            End If
        Catch ex As Exception
            i = defaultValue
        End Try

        Return i
    End Function
    Public Shared Function GetValidString(ByVal obj As Object, _
                   Optional ByVal defaultValue As String = "") As String
        Dim s As String
        Try
            If (obj Is Nothing) Then
                s = defaultValue
            Else
                s = obj.ToString()
            End If
        Catch
            s = defaultValue
        End Try

        Return s
    End Function
    Public Shared Function SafeDivide(ByVal dividend As Double, ByVal divisor As Double, Optional ByVal defaultValue As Double = 0) As Double

        If divisor = 0 Then
            Return defaultValue
        Else
            Return (dividend / divisor)
        End If
    End Function
    Public Shared Function GetCountry(ByVal state As String) As String
        Dim retval As String = ""
        Dim accessCon As New OleDbConnection(ConfigurationManager.ConnectionStrings("GotPinkDB").ConnectionString)
        accessCon.Open()
        Dim query As String = "select Country from StateProvinces where Abbreviation = @State"
        Dim accessCom As New OleDbCommand(query, accessCon)
        Dim param1 As OleDbParameter = accessCom.Parameters.Add("@State", OleDbType.VarChar, 50)
        param1.Value = state
        Dim dr As OleDbDataReader = accessCom.ExecuteReader()
        Try
            While (dr.Read)
                retval = Utility.GetValidString(dr("Country"))
            End While
        Catch ex As Exception

        Finally
            dr.Close()
            accessCom.Dispose()
            accessCon.Close()
        End Try
        Return retval
    End Function
    Public Shared Function GetMaskedString(ByVal tmp As String, ByVal num As Integer) As String
        If num > tmp.Length Then
            num = tmp.Length - 1
        End If
        Return New String("*", tmp.Length - num) & Right(tmp, num)
    End Function
End Class
